from urllib import request
from xml.dom import minidom
from procutil import Task
import uuid, re

class DownloadCategory0:
    def __init__(self,Fn):
        self.fn = Fn
		
    def __call__(self,args):
        f = request.urlopen(args)
        doc = minidom.parse(f)
        urls = doc.getElementsByTagName("Url")
        loc = "http://test.myolive.net/samples/wmm/made-in-china.com/category1.wmm?OrangeUrl=http://www.made-in-china.com"
        for url in urls:
            if url.hasChildNodes():
                task = Task();
                task.guid = uuid.uuid4()
                task.task_type = 1
                task.name = "Directory.DownloadCategory1"
                task.method = self.fn
                task.arg = loc + url.firstChild.nodeValue
                task.Create()


class DownloadCategory1:
    def __init__(self,Fn):
        self.fn = Fn
		
    def __call__(self,args):
        f = request.urlopen(args)
        doc = minidom.parse(f)
        urls = doc.getElementsByTagName("Url")
        loc = "http://test.myolive.net/samples/wmm/made-in-china.com/category2.wmm?OrangeUrl=http://www.made-in-china.com"
        for url in urls:
            if url.hasChildNodes():
                task = Task();
                task.guid = uuid.uuid4()
                task.task_type = 1
                task.name = "Directory.DownloadCategory2"
                task.method = self.fn
                task.arg = loc + url.firstChild.nodeValue
                task.Create()


class DownloadCategory2:
    def __init__(self,Fn):
        self.fn = Fn
		
    def __call__(self,args):
        f = request.urlopen(args)
        doc = minidom.parse(f)
        urls = doc.getElementsByTagName("Url")
        loc = "http://test.myolive.net/samples/wmm/made-in-china.com/directory_links.wmm?OrangeUrl=http://www.made-in-china.com"
        for url in urls:
            if url.hasChildNodes():
                task = Task();
                task.guid = uuid.uuid4()
                task.task_type = 1
                task.name = "Directory.DownloadDirectoryLinks"
                task.method = self.fn
                task.arg = loc + url.firstChild.nodeValue
                task.Create()


class DownloadDirectoryLinks:
    def __init__(self,Fn):
        self.fn = Fn
		
    def __call__(self,args):
        f = request.urlopen(args)
        doc = minidom.parse(f)
        comLists = doc.getElementsByTagName("CompanyList")
        loc = "http://test.myolive.net/samples/wmm/made-in-china.com/directory.wmm?OrangeUrl=http://www.made-in-china.com"
        for com in comLists:
            if com.hasChildNodes() and com.firstChild.nodeValue!='':
                url = com.firstChild.nodeValue.strip(' \t\n')
                if url!='':
                    for i in range(1,4):
                        task = Task();
                        task.guid = uuid.uuid4()
                        task.task_type = 1
                        task.name = "Directory.DownloadDirectories"
                        task.method = self.fn
                        task.arg = loc + url.replace("1.html", "%s.html"%i)
                        task.Create()               
        

class DownloadDirectories:
    def __init__(self,Fn):
        self.fn = Fn
		
    def __call__(self,args):
        f = request.urlopen(args)
        doc = minidom.parse(f)
        urls = doc.getElementsByTagName("Url")
        loc = "http://test.myolive.net/samples/wmm/made-in-china.com/company_links.wmm?OrangeUrl=http://www.made-in-china.com"
        for url in urls:
            if url.hasChildNodes() and url.firstChild.nodeValue!='':
                path = url.firstChild.nodeValue.strip(' \t\n')
                if path !='':
                    task = Task();
                    task.guid = uuid.uuid4()
                    task.task_type = 1
                    task.name = "Directory.DownloadCompanyLinks"
                    task.method = self.fn
                    task.arg = loc + path
                    task.Create()               
        

class DownloadCompanyLinks:
    def __init__(self,Fn):
        self.fn = Fn
		
    def __call__(self,args):
        f = request.urlopen(args)
        doc = minidom.parse(f)
        premSite = doc.getElementsByTagName("PremiumSite")
        preSite_loc = "http://test.myolive.net/samples/wmm/made-in-china.com/company_links.wmm?OrangeUrl=http://www.made-in-china.com"
        urls = doc.getElementsByTagName("CompanyInfoUrl")
        loc = "http://test.myolive.net/samples/wmm/made-in-china.com/profile.wmm?OrangeUrl=http://www.made-in-china.com"
        task = Task();
        task.guid = uuid.uuid4()
        task.task_type = 1
        task.name = "Directory.DownloadProfile"
        task.method = self.fn
        task.arg = loc + urls[0].firstChild.nodeValue
        task.Create()

        urls = doc.getElementsByTagName("ProductListUrl")
        loc = "http://test.myolive.net/samples/wmm/made-in-china.com/products.wmm?OrangeUrl=http://www.made-in-china.com"
        task = Task();
        task.guid = uuid.uuid4()
        task.task_type = 1
        task.name = "Directory.DownloadProducts"
        task.method = self.fn
        task.arg = loc + urls[0].firstChild.nodeValue
        task.Create()

        urls = doc.getElementsByTagName("ContactDetailsUrl")
        loc = "http://test.myolive.net/samples/wmm/made-in-china.com/contacts.wmm?OrangeUrl=http://www.made-in-china.com"
        task = Task();
        task.guid = uuid.uuid4()
        task.task_type = 1
        task.name = "Directory.DownloadContactDetails"
        task.method = self.fn
        task.arg = loc + urls[0].firstChild.nodeValue
        task.Create()              

class DownloadProfile:
    def __init__(self,Fn):
        self.fn = Fn
		
    def __call__(self,args):
        f = request.urlopen(args)
        doc = minidom.parse(f)
        urls = doc.getElementsByTagName("")
        loc = "http://test.myolive.net/samples/wmm/made-in-china.com/company_links.wmm?OrangeUrl=http://www.made-in-china.com"
        for url in urls:
            task = Task();
            task.guid = uuid.uuid4()
            task.task_type = 1
            task.name = "Directory.DownloadCompanies"
            task.method = self.fn
            if url.hasChildNodes():
                task.arg = loc + url.firstChild.nodeValue
                task.Create()

class DownloadProducts:
    def __init__(self,Fn):
        self.fn = Fn
		
    def __call__(self,args):
        f = request.urlopen(args)
        doc = minidom.parse(f)
        urls = doc.getElementsByTagName("")
        loc = "http://test.myolive.net/samples/wmm/made-in-china.com/company_links.wmm?OrangeUrl=http://www.made-in-china.com"
        for url in urls:
            task = Task();
            task.guid = uuid.uuid4()
            task.task_type = 1
            task.name = "Directory.DownloadCompanies"
            task.method = self.fn
            if url.hasChildNodes():
                task.arg = loc + url.firstChild.nodeValue
                task.Create()

class DownloadContacts:
    def __init__(self,Fn):
        self.fn = Fn
		
    def __call__(self,args):
        f = request.urlopen(args)
        doc = minidom.parse(f)
        urls = doc.getElementsByTagName("")
        loc = "http://test.myolive.net/samples/wmm/made-in-china.com/company_links.wmm?OrangeUrl=http://www.made-in-china.com"
        for url in urls:
            task = Task();
            task.guid = uuid.uuid4()
            task.task_type = 1
            task.name = "Directory.DownloadCompanies"
            task.method = self.fn
            if url.hasChildNodes():
                task.arg = loc + url.firstChild.nodeValue
                task.Create()
           
if __name__ == "__main__":
    ''' cat0 = DownloadCategory0("DownloadCategory1")
    cat0("http://test.myolive.net/samples/wmm/made-in-china.com/category0.wmm");
    cat2 = DownloadCategory2("DownloadCategory2")
    cat2("http://test.myolive.net/samples/wmm/made-in-china.com/category1.wmm?OrangeUrl=http://www.made-in-china.com/products/catlist/listsubcat/123/00/mic/Electrical_Electronics.html");
    
    dlink = DownloadDirectoryLinks("DownloadDirectories")
    dlink("http://test.myolive.net/samples/wmm/made-in-china.com/directory_links.wmm?OrangeUrl=http://www.made-in-china.com/Electrical-Electronics-Catalog/Transducer.html")
    
    di = DownloadDirectories("DownloadCompanyLinks")
    di("http://test.myolive.net/samples/wmm/made-in-china.com/directory.wmm?OrangeUrl=http://www.made-in-china.com/manufacturers-directory/item3/Transducer-1.html")
    '''
    cl = DownloadCompanyLinks("DownloadContacts")
    cl("http://test.myolive.net/samples/wmm/made-in-china.com/company_links.wmm?OrangeUrl=http://www.made-in-china.com/showroom/chinahapn")
